<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$merchid=intval($_RQ['merchid']);
	if($merchid || !$_RQ['ismerch']){
		$this->session('merchid',$merchid);
	}
	if($_RQ['ismerch']){
		$merchid=$_SESSION['merchid'];
		$this->session('merchid',$merchid);
	}
	$item=array('type'=>'0','groupid'=>'0');
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'set',
		'desc'=>'商品导出',
		'edit'=>array(
			array('type'=>'tip','title'=>'只有导出分类和商品，才是完整的！<red>导出的商品是完整的，包括在售商品，售罄商品，回收站里面的所有商品！</red>'),
			array('type'=>'radio','title'=>'导出类型','name'=>'type','options'=>'types'),
			array('type'=>'radio','title'=>'商品状态','name'=>'status','options'=>'status','show'=>array('type'=>1)),
			array('type'=>'switch','title'=>'批量修改导出','name'=>'is_up','show'=>array('type'=>1),'help'=>'用于批量导出商品，然后批量修改后，再导入商品，达到批量修改的目的！'),
			array('type'=>'select','title'=>'导出分类？','name'=>'groupid','options'=>'groups','show'=>array('type'=>1)),
		)
	);
	$return['status']=array('0'=>'下架','1'=>'上架','2'=>'售罄','3'=>'回收站');
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'parentid'=>0);
	$return['groups']=pdo_getall('jy_weishop_goods_group',$groupspram,array('id','title'),'id');
	$return['groups'][0]=array('id'=>0,'title'=>'全部分类');
	$return['htmls']=$htmls;
	$return['types']=array(
		'0'=>'导出分类',
		'1'=>'导出商品',
		'2'=>'导出门店',
	);	
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();	
	$_SESSION['e2cpost']=$post;
	if($post['type']==1){
		$url=download_url('jy_weishopniu/goods_chu/down/groupid='.$post['groupid'].'&is_up='.$post['is_up'].'&status='.$post['status']);
	}elseif($post['type']==2){
		$url=download_url('jy_weishopniu/goods_chu/downstore/');
	}else{
		$url=download_url('jy_weishopniu/goods_chu/downgroup/');
	}
	exi('点确定开始下载','','',$url);
	
}
if($_RQ['op']=='downgroup'){
	$this->model('chu')->down_group();
}
if($_RQ['op']=='downstore'){
	$this->model('chu')->down_store();
}
if($_RQ['op']=='down'){
	$filename='candel/'.random(15);
	@mkdir(ROOT_AT.'candel/');
	@mkdir(ROOT_AT.$filename);
	$groupid=intval($_RQ['groupid']);
	$is_up=intval($_RQ['is_up']);
	$status=intval($_RQ['status']);
	$readme='[温馨提示]：商品文件，请勿多次重复导入，否则会出现数据重复！';
	if($is_up){
		$readme .='【当前属于批量修改导出，只导出的一部分可批量修改的商品信息！】';
	}
	file_put_contents(ROOT_AT.$filename.'/readme.txt',$readme);
	$merchid=$_SESSION['merchid']?:0;
	$this->model('chu')->down_goods($filename,$groupid,$merchid,$is_up,$status);
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	if($groupid){
		$params['pcate']=$groupid;
	}
	$goodsids=pdo_getall('jy_weishop_goods',$params,'id','id');
	if(count($goodsids)>0){
		$goodsids=array_keys($goodsids);
		$this->model('chu')->down_dispatch($filename,$is_up);
		$this->model('chu')->down_option($filename,$goodsids,$is_up);
		$this->model('chu')->down_spec($filename,$goodsids,$is_up);
		$this->model('chu')->down_spec_item($filename,$goodsids,$is_up);
	}
	//规格
	cfc('tools')->down_zip(ROOT_AT.$filename,$filename,true);
	exit;
}
